package com.yiren.dbaa.module.project_edit.view.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.project_edit.contract.SingleLightEditContract;
import com.yiren.dbaa.module.project_edit.presenter.SingleLightEditPresenter;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/8/24 17:33
 * @description: 单项灯光编辑
 */
public class SingleLightEdit_Activity extends BaseActivity implements SingleLightEditContract.View{


    @BindView(R.id.tv_title)
    TextView tvTitle;//标题
    @BindView(R.id.et_wait_time)
    EditText etWaitTime;//灯光等待时间
    @BindView(R.id.et_voice_content)
    EditText etVoiceContent;//灯光内容
    @BindView(R.id.tv_audition)
    TextView tvAudition;//试听按钮
    @BindView(R.id.tv_select_file)
    TextView tvSelectFile;//选择音频文件按钮
    @BindView(R.id.tv_judge_operation)
    TextView tvJudgeOperation;//评判操作下拉框
    @BindView(R.id.cb_driving_light)
    CheckBox cbDrivingLight;//行车灯cb
    @BindView(R.id.radiogroup_driving_light)
    RadioGroup radiogroupDrivingLight;//行车灯开关
    @BindView(R.id.cb_dipped_light)
    CheckBox cbDippedLight;//近光灯cb
    @BindView(R.id.radiogroup_dipped_light)
    RadioGroup radiogroupDippedLight;//近光灯开关
    @BindView(R.id.cb_high_light)
    CheckBox cbHighLight;//远光灯cb
    @BindView(R.id.radiogroup_high_light)
    RadioGroup radiogroupHighLight;//远光灯开关
    @BindView(R.id.cb_fog_light)
    CheckBox cbFogLight;//雾灯cb
    @BindView(R.id.radiogroup_fog_light)
    RadioGroup radiogroupFogLight;//雾灯开关
    @BindView(R.id.cb_far_near)
    CheckBox cbFarNear;//远近交替cb
    @BindView(R.id.et_far_ner_counts)
    EditText etFarNerCounts;//远近交替次数
    @BindView(R.id.cb_left_light)
    CheckBox cbLeftLight;//左转灯cb
    @BindView(R.id.radiogroup_left_light)
    RadioGroup radiogroupLeftLight;//左转灯开关
    @BindView(R.id.cb_right_light)
    CheckBox cbRightLight;//右转灯cb
    @BindView(R.id.radiogroup_right_light)
    RadioGroup radiogroupRightLight;//右转灯开关
    @BindView(R.id.cb_warning_light)
    CheckBox cbWarningLight;//警示灯cb
    @BindView(R.id.radiogroup_warning_light)
    RadioGroup radiogroupWarningLight;//警示灯开关
    @BindView(R.id.cb_farandnear_light)
    CheckBox cbFarandnearLight;//近光或远光
    @BindView(R.id.ll_judge_light)
    LinearLayout llJudgeLight;//评判操作选项布局
    @BindView(R.id.tv_points_content)
    TextView tvPointsContent;//扣分内容下拉框
    @BindView(R.id.tv_points)
    TextView tvPoints;//分数下拉框
    @BindView(R.id.et_points_content)
    EditText etPointsContent;//扣分内容编辑框
    @BindView(R.id.ll_save_light)
    LinearLayout llSaveLight;//保存
    private SingleLightEditContract.Presenter  singleLightEditPresenter;
    private GradeStandardEntity gradeStandard;//选择评判模板
    private int grad = 100;//扣分初始值
    private SiteEntity site;//当前操作的考场
    private SingleLightEntity singleLight;//操作的单项灯光
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singlelight_setting);
        ButterKnife.bind(this);
        tvTitle.setText("单项灯光编辑");
        Intent getIntent = getIntent();
        int intExtra =  (Integer)getIntent.getIntExtra("singleLight",-1);
        site = (SiteEntity) getIntent.getSerializableExtra("site");
        //初始化
        init();
        if (intExtra >=0 ) {
            singleLight =  singleLightEditPresenter.getAllSingleLightList().get(intExtra);
            gradeStandard = singleLight.getGradeStandardEntity();
            loadModifyContent();
        }

    }

    private void init() {
        singleLightEditPresenter = new SingleLightEditPresenter(this);
    }


    @OnClick({R.id.iv_back, R.id.ll_save_light, R.id.tv_audition, R.id.tv_points, R.id.tv_points_content})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back://返回
                finish();
                break;
            case R.id.tv_audition://声音测试
                MessageBox.VoiceModel(this, etVoiceContent.getText().toString());
                break;
            case R.id.tv_points_content://选择扣分内容
                singleLightEditPresenter.loadGradeStandardDialog(this,2, "请选择扣分内容");
                break;
            case R.id.tv_points://选择扣分分数
                singleLightEditPresenter.loadGradeStandardDialog(this,3, "请选择扣分");
                break;
            case R.id.ll_save_light://保存编辑内容
                String time = etWaitTime.getText().toString();
                String lightContent = etVoiceContent.getText().toString();
                String gradeStandarContent = etPointsContent.getText().toString();
                singleLightEditPresenter.saveSingleLight(site,time,lightContent,gradeStandard,gradeStandarContent,grad,getLightAction(),singleLight);
                break;
        }
    }

    /**加载修改值*/
    public void loadModifyContent() {
        try {
            etWaitTime.setText(String.valueOf(singleLight.getWaitTime()));
            etVoiceContent.setText(singleLight.getContent());
            tvJudgeOperation.setText(singleLight.getStateValueShow());
            if (singleLight.getGradeStandardEntity() != null) {
                if (singleLight.getGradeStandardEntity().getProjectId()!=null){
                    tvPointsContent.setText(singleLight.getGradeStandardEntity().getStandardContent());
                    etPointsContent.setVisibility(View.GONE);
                    tvPoints.setCompoundDrawables(null,null,null,null);
                    tvPoints.setText("扣"+singleLight.getGradeStandardEntity().getGrade()+"分");
                    tvPoints.setClickable(false);
                }else {
                    tvPointsContent.setText("请选择内容");
                    etPointsContent.setVisibility(View.VISIBLE);
                    etPointsContent.setText(singleLight.getGradeStandardEntity().getStandardContent());
                    tvPoints.setText("扣"+singleLight.getGradeStandardEntity().getGrade()+"分");
                    Drawable drawable = getResources().getDrawable(R.mipmap.arrow_down);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    tvPoints.setCompoundDrawables(null,null,drawable,null);
                    tvPoints.setClickable(true);
                }
            }
            HashMap<String, String> lightstate = new LinkedHashMap<>();
            lightstate = new Gson().fromJson(singleLight.getStateValue(), LinkedHashMap.class);
            setLightAction(lightstate);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showPointsContent(String selectString,GradeStandardEntity gradeStandard,boolean isCustom) {
        this.gradeStandard = gradeStandard;
        if(isCustom){
            tvPointsContent.setText("请选择内容");
            etPointsContent.setVisibility(View.VISIBLE);
            Drawable drawable = getResources().getDrawable(R.mipmap.arrow_down);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvPoints.setCompoundDrawables(null,null,drawable,null);
            tvPoints.setClickable(true);
        }else {
            tvPointsContent.setText(gradeStandard.getStandardContent());
            etPointsContent.setVisibility(View.GONE);
            tvPoints.setCompoundDrawables(null,null,null,null);
            tvPoints.setText("扣"+gradeStandard.getGrade()+"分");
            tvPoints.setClickable(false);
        }
    }

    @Override
    public void showPoints(int grade) {
        this.grad = grade;
        tvPoints.setText("扣"+grade+"分");
    }


    /**
     * 设置灯光操作状态
     * @param lightMap
     */
    private void setLightAction(Map<String, String> lightMap) {
        for (String key : lightMap.keySet()) {
            switch (key) {
                case "DRIVING_LAMP":
                    cbDrivingLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupDrivingLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupDrivingLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "PASSING_LIGHT":
                    cbDippedLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupDippedLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupDippedLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "DISTANCE_LIGHT":
                    cbHighLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupHighLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupHighLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "FOGLIGHT":
                    cbFogLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupFogLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupFogLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "DISTANCE_LIGHT_ALTERNATE":
                    cbFarandnearLight.setChecked(true);
                    etFarNerCounts.setText(lightMap.get(key));
                    break;
                case "LEFT_LIGHT":
                    cbLeftLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupLeftLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupLeftLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "RIGHT_LIGHT":
                    cbRightLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupRightLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupRightLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "CAUTION_LIGHT":
                    cbWarningLight.setChecked(true);
                    if (lightMap.get(key).contains("OPEN")) {
                        RadioButton rb = (RadioButton) radiogroupWarningLight.getChildAt(0);
                        rb.setChecked(true);
                    } else {
                        RadioButton rb = (RadioButton) radiogroupWarningLight.getChildAt(1);
                        rb.setChecked(true);
                    }
                    break;
                case "PASSING_OR_DISTANCE_LIGHT":
                    cbFarandnearLight.setChecked(true);
                    break;
            }

        }
    }

    /**
     * 获取灯光操作项
     */
    private HashMap<String, String>  getLightAction() {
        HashMap<String, String> lightValue = new LinkedHashMap<>();//存放灯光值
        //行车灯
        if (cbDrivingLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupDrivingLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.DRIVING_LAMP.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.DRIVING_LAMP.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }
        }

        //近光灯
        if (cbDippedLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupDippedLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.PASSING_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.PASSING_LIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }
        }

        //远光灯
        if (cbHighLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupHighLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.DISTANCE_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.DISTANCE_LIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }

        }

        //雾灯
        if (cbFogLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupFogLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.FOGLIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.FOGLIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }
        }

        //远近交替
        if (cbFarNear.isChecked()) {
            if (!etFarNerCounts.getText().toString().equals("")) {
                lightValue.put(CarStateNameEnum.DISTANCE_LIGHT_ALTERNATE.toString(), etFarNerCounts.getText().toString());
            }

        }

        //左转灯
        if (cbLeftLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupLeftLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.LEFT_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.LEFT_LIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }

        }

        //右转灯
        if (cbRightLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupRightLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.RIGHT_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.RIGHT_LIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }

        }

        //警示灯
        if (cbWarningLight.isChecked()) {
            RadioButton radioButton = (RadioButton) findViewById(radiogroupWarningLight.getCheckedRadioButtonId());
            if (radioButton != null) {
                if (radioButton.getText().equals("开")) {
                    lightValue.put(CarStateNameEnum.CAUTION_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
                } else {
                    lightValue.put(CarStateNameEnum.CAUTION_LIGHT.toString(), CarStateValueEnum.CLOSE.toString());
                }
            }

        }
        //近光或远光
        if (cbFarandnearLight.isChecked()) {
            lightValue.put(CarStateNameEnum.PASSING_OR_DISTANCE_LIGHT.toString(), CarStateValueEnum.OPEN.toString());
        }

        return  lightValue;
    }



}
